﻿


using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace Ls.ImageSharep.App
{
    public partial class UCMerge : UserControl
    {
        public UCMerge()
        {
            InitializeComponent();
        }

        private void UCMerge_Load(object sender, EventArgs e)
        {
            picbox1.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/avatar.png");
            this.picbox1.SizeMode = PictureBoxSizeMode.StretchImage;

            picbox2.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/bg.jpg");
            this.picbox2.SizeMode = PictureBoxSizeMode.StretchImage;

            //picbox3.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/invoice.png");
            //this.picbox1.SizeMode = PictureBoxSizeMode.AutoSize;

            txtx.Text = "10";
            txty.Text = "20";
        }

    
        private void btnxt_Click(object sender, EventArgs e)
        {
            MyOpenFileDialog(1);

            
        }
        //合成
        private void btnyl_Click(object sender, EventArgs e)
        {
           
            var streamdata = MergeImage(picbox2.Image, picbox1.Image,Convert.ToInt32(txtx.Text), Convert.ToInt32(txty.Text),ckbyuan.Checked);
            picbox3.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(streamdata));
            this.picbox3.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void btndt_Click(object sender, EventArgs e)
        {
            MyOpenFileDialog(2);
        }

        #region 选择对话框
        public void MyOpenFileDialog(int type)
        {
            //定义一个文件打开控件
            OpenFileDialog ofd = new OpenFileDialog();

            //设置打开对话框的标题
            ofd.Title = "请选择要打开的文件";
            //设置打开对话框可以多选
            ofd.Multiselect = false;
            //设置对话框打开的文件类型
            ofd.Filter = "图片文件|*.jpeg|图片文件|*.png|图片文件|*.jpg";
            //设置文件对话框当前选定的筛选器的索引
            ofd.FilterIndex = 2;
            //设置对话框是否记忆之前打开的目录
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的文件完整路径
                string filePath = ofd.FileName;
                //获取对话框中所选文件的文件名和扩展名，文件名不包括路径
                string fileName = ofd.SafeFileName;

                using (FileStream fsRead = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    if (type ==1)
                    {
                        picbox1.Image = System.Drawing.Image.FromStream(fsRead);
                    }
                    else if (type==2)
                    {
                        picbox2.Image = System.Drawing.Image.FromStream(fsRead);
                    }
                  
                }
            }
        }

        /// <summary>
        /// 合并图片
        /// </summary>
        /// <param name="templateImage"></param>
        /// <param name="mergeImagePath">合并图片</param>
        /// <param name="x">X坐标</param>
        /// <param name="y">y坐标</param>
        /// <returns></returns>
        public static SixLabors.ImageSharp.Image  MergeImage(System.Drawing.Image templateImage, System.Drawing.Image mergeImage, int x, int y,bool chkyuan)
        {
            // 加载需要合并的图片
            SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(LsApp.ImgToByt(templateImage));
            using (SixLabors.ImageSharp.Image mergeimage = SixLabors.ImageSharp.Image.Load(LsApp.ImgToByt(mergeImage)))
            {
                if (chkyuan)
                {
                    //Image destRound = img.Clone(x => x.ConvertToAvatar(new Size(200, 200), 100))
                    mergeimage.Mutate(x=>x.ConvertToAvatar(new SixLabors.ImageSharp.Size(200, 200), 100));
                    mergeimage.Save("imag.png");
                }
                image.Mutate(o =>
                {
                    o.DrawImage(mergeimage, new SixLabors.ImageSharp.Point(x, y), 1);
                });
            }
               

            return image;

        }

     
        #endregion

    }
}
